package cn.edu.ccibe.exam16system.controller;

import cn.edu.ccibe.exam16system.entity.form.LoginForm;
import cn.edu.ccibe.exam16system.service.StudentService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import jakarta.websocket.server.PathParam;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class StudentLoginController {

    @Resource
    private StudentService studentService;

    @GetMapping("/login")
    public String login(@PathParam("admin") String admin){
        if(admin == null) admin = "";
        if("true".equals(admin)) return "index";
        return "login";
    }

    @PostMapping("/doLogin")
    @ResponseBody
    public String doLogin(HttpServletRequest request, LoginForm form){
        String username = form.getUsername();
        String password = form.getPassword();

        String result =studentService.login(username,password);
        HttpSession session = request.getSession();
        if(!result.isEmpty()) session.setAttribute("loginUsername",result);
        return result;
    }

}
